package cn.com.egova.permission;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import cn.com.egova.permission.google.GoogleActivity;
import cn.com.egova.permission.dispatcher.DispatcherActivity;
import cn.com.egova.permission.inherit.InheritMethodActivity;
import cn.com.egova.permission.relative.RelativeMethodActivity;
import cn.com.egova.permission.szzt.SZZTActivity;

/**
 * 本例以多种方式动态申请权限,这里以打电话的权限为例:
 * <uses-permission android:name="android.permission.CALL_PHONE"/>
 */
public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
    private ListView listView;
    private String[] data = new String[]{"基本方式","继承方式","关联方式","google提供的封装类","PermissionsDispatcher框架","数字政通方式"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initActionBar();

        initView();
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setTitle("Android 6.0动态申请权限");
        }
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }else{
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = null;
        switch (position) {
            case  0:
                intent = new Intent(this,BasicMethodActivity.class);
                break;
            case  1:
                intent = new Intent(this,InheritMethodActivity.class);
                break;
            case  2:
                intent = new Intent(this,RelativeMethodActivity.class);
                break;
            case  3:
                intent = new Intent(this,GoogleActivity.class);
                break;
            case  4:
                intent = new Intent(this,DispatcherActivity.class);
                break;
            case  5:
                intent = new Intent(this,SZZTActivity.class);
                break;
        }
        intent.putExtra("title",data[position]);
        startActivity(intent);
    }
}
